package com.janj.Druid_Demo;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Properties;

//采用 Druid 连接池
public class Demo1 {
    public static void main(String[] args) throws Exception {
        //1.准备类加载器
        ClassLoader loader = ClassLoader.getSystemClassLoader();
        InputStream is = loader.getResourceAsStream("druid.properties");

        //2.准备Properties
        Properties pp = new Properties();
        pp.load(is);

        //3.工厂类,得到连接池管理者
        DataSource ds = DruidDataSourceFactory.createDataSource(pp);

        //4.获取连接
        Connection conn = ds.getConnection();

        //---------------
        //1.获取到执行者
        String sql = "UPDATE account SET money = money -100 where id = ? ";
        PreparedStatement ps = conn.prepareStatement(sql);

        //2.执行SQL语句
        ps.setInt(1, 2);
        int lineNumber = ps.executeUpdate();

        //3.使用结果
        System.out.println("lineNumber = " + lineNumber);
        System.out.println(lineNumber > 0 ? "罚款成功" : "罚款失败");

        //4.释放资源,归还连接
        ps.close();
        conn.close();
    }
}
